package com.szh.zx.java8.day01;


import com.szh.zx.java8.POJO.Employee;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class TestLambda {
	// 原来的匿名内部类
	public void test(){
		Comparator<Integer> comparable = new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return Integer.compare(o1,o2);
			}

		};
	}
	// 有了lambda 表达式之后
	public void test2(){
		Comparator<Integer> comparator = (x,y) ->Integer.compare(x,y);
	}
	List<Employee> employees = Arrays.asList(
			new Employee("张三1",12,2222),
			new Employee("张三2",12,2222),
			new Employee("张三3",40,2222),
			new Employee("张三4",12,22222),
			new Employee("张三5",12,2222)
	);
	// 需求，获取当前公司中员工年龄大于35的员工信息
	public List<Employee> filterEmployees(List<Employee> list){
		List<Employee> employees = new ArrayList<>();
		for (Employee emp: list){
			if (emp.getAge()>=35){
				employees.add(emp);
			}
		}
		return employees;
	}
	
	public void test3(){
		List<Employee> list = filterEmployees(employees);
		for (Employee employee : list) {
			System.out.println(employee);
		}
	}
	// 需求2： 求工资大于3000的
	public List<Employee> filterEmployees2(List<Employee> list){
		List<Employee> employees = new ArrayList<>();
		for (Employee emp: list){
			if (emp.getSalary()>=6000){
				employees.add(emp);
			}
		}
		return employees;
	}
	// 测试
	
	public void test4(){
		List<Employee> list = filterEmployees2(employees);
		for (Employee employee : list) {
			System.out.println(employee);
		}
	}
	// 优化一：抽出公共部分，公共接口，让每个需求创建个类去实现，这是策略模式，但是每次都需要创建新的类
	List<Employee> filterEmployee(List<Employee> list,MyPredicate<Employee> myP){
		List<Employee> employees = new ArrayList<>();
		for (Employee emp: list){
			if (myP.test(emp)){
				employees.add(emp);
			}
		}
		return employees;
	}
	// 测试优化
	void test5(){
		List<Employee> list =filterEmployee(employees,new FilterEmployeeByAge());
		for (Employee employee : list) {
			System.out.println(employee);
		}
	}
	// 优化2：匿名内部类
	
	void  test6(){
		List<Employee> list = filterEmployee(this.employees, new MyPredicate<Employee>() {
			@Override
			public boolean test(Employee t) {
				return t.getSalary() <= 5000;
			}
		});
		for (Employee employee : list) {
			System.out.println(employee);
		}
	}
	// 优化三：Lambda表达式
	
	void test7(){
		List<Employee> list = filterEmployee(employees,(e)-> e.getSalary()>=2000);
		list.forEach(System.out::println);
	}
	// 优化方式四:可以把接口去掉，实现类也去电，其他方法也去掉，只留下插入数据的东西
	// 这就是Stream API

	
	public void test8(){
		employees.stream()
				.filter((e)->e.getSalary()>=3000)
				.limit(2)
				.forEach(System.out::println);
	}
}
